package com.jd.dl.web;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.jd.dl.pojo.User;
import com.jd.dl.service.UserService;
import com.jd.dl.service.exception.ErrorCode;
import com.jd.dl.service.exception.ServiceException;
import com.jd.dl.web.interceptor.LoginInterceptor;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements ServletResponseAware,
		ServletRequestAware, SessionAware, CookiesAware {

	private String phoneNumber;
	private String password;

	private UserService userService;

	private boolean rememberMe;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private Map session;
	private Map cookies;
	private String goingToURL;

	public String getGoingToURL() {
		return goingToURL;
	}

	public void setGoingToURL(String goingToURL) {
		this.goingToURL = goingToURL;
	}

	public boolean isRememberMe() {
		return rememberMe;
	}

	public void setRememberMe(boolean rememberMe) {
		this.rememberMe = rememberMe;
	}

	public String login() {

		try {
			Object obj=session.get(LoginInterceptor.USER_SESSION_KEY);
			if(obj!=null&&!(obj instanceof User)){
				throw new ServiceException(ErrorCode.USER_NOT_EXIST,"请使用其他浏览器或关闭浏览器重新登陆！");
			}
			User user = userService.login(phoneNumber, password);
			if (rememberMe) {
				Cookie cookie = new Cookie(
						LoginInterceptor.COOKIE_REMEMBERME_KEY, user
								.getPhoneNumber()
								+ "==" + user.getPassword());
				cookie.setMaxAge(60 * 60 * 24 * 14);
				response.addCookie(cookie);
			}
			session.put(LoginInterceptor.USER_SESSION_KEY, user);
			String goingToURL = (String) session
					.get(LoginInterceptor.GOING_TO_URL_KEY);
			//setDefaultFunction(user);
			if (goingToURL!=null&&!"".equals(goingToURL)) {
				//setGoingToURL(goingToURL);
				setGoingToURL("showMain");
				session.remove(LoginInterceptor.GOING_TO_URL_KEY);
			} else {
				setGoingToURL("showMain");
			}
			return SUCCESS;
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}

	}
	
//	private void setDefaultFunction(User user) {
//		List<Function> functions=new ArrayList<Function>();
//		if(user.getRole().isAdmin()){
//			functions=functionService.getSubTree();
//		}else{
//			functions=roleService.loadFunctionsByeRoleId(user.getRole().getRoleId());
//		}
//		
//		for (Function function : functions) {
//			if(function.getIsDefault()){
//				this.session.put(SessionKey.DEFAULT_FUNCTION, function);
//				return;
//			}
//		}
//		this.session.put(SessionKey.DEFAULT_FUNCTION, functions.get(0));
//		
//	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setCookiesMap(Map cookies) {
		this.cookies = cookies;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	

}
